package fms.webservice.server.dao;

import fms.webservice.server.util.ConfUtil;
import org.apache.log4j.Logger;

import java.io.IOException;
import java.sql.*;
import java.util.List;


public class DataReaderDao {
    private static Logger logger = Logger.getLogger(DataReaderDao.class);
    private Connection conn = null;
    private ResultSet rs = null;
    private List listRS = null;
    private Statement stmt = null;
    private static final String DBDRIVER = ConfUtil.getConf("/db.properties").getProperty("driverClassName");
    private static final String ORADBURLPREFIX = ConfUtil.getConf("/db.properties").getProperty("url");

    public DataReaderDao() throws ClassNotFoundException, SQLException {
        Class.forName(DBDRIVER);
        try {
            conn = DriverManager.getConnection(ORADBURLPREFIX,
                    ConfUtil.getConf("/db.properties").getProperty("username"),
                    ConfUtil.getConf("/db.properties").getProperty("password"));
            stmt = conn.createStatement();
        } catch (SQLException e) {
            logger.error("创建数据库连接出错!" + e.getMessage());
        }
    }


    public List getRawMsgList(String strSql) throws ClassNotFoundException, IOException {
        try {
            ResultSet rs = stmt.executeQuery(strSql);
            listRS = (List) ResultSetToList.resultSetToList(rs);
        } catch (SQLException e) {
            logger.error("获取风温数据出错!\n" + e.getMessage());
        }
        return listRS;
    }

    public boolean validate(String strSql) {
        try {
            ResultSet rs = stmt.executeQuery(strSql);
            return rs==null?false:true;
        } catch (SQLException e) {
            logger.error("验证用户名和密码出错!\n" + e.getMessage());
            return false;
        }
    }

    protected void finalize() {
        try {
            rs.close();
        } catch (SQLException e) {
            logger.error("释放ResultSet出错!\n" + e.getMessage());
        }
        try {
            stmt.close();
        } catch (SQLException e) {
            logger.error("释放Statemnet出错!\n" + e.getMessage());

        }
        try {
            conn.close();
        } catch (SQLException e) {
            logger.error("释放Connection出错!\n" + e.getMessage());
        }
    }
}
